package com.yequan.hbb.view.fragment.lhp;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yequan.hbb.R;
import com.yequan.hbb.adapter.DetailAdapter;
import com.yequan.hbb.adapter.LearnAdapter;
import com.yequan.hbb.adapter.TypeAdapter;
import com.yequan.hbb.app.MyApplication;
import com.yequan.hbb.entries.SearchEntry;
import com.yequan.hbb.entries.TypeListEntry;
import com.yequan.hbb.servers.Servers;
import com.yequan.hbb.utils.ServerUtil;
import com.yequan.hbb.view.fragment.LearnHBFragment;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class TypeFragment extends Fragment {


    private RecyclerView recyclerView;
    private RecyclerView recyclerView_detail;
    private TypeAdapter typeAdapter;
    private DetailAdapter detailAdapter;

    public TypeFragment() {
        // Required empty public constructor
    }
    public static TypeFragment instance(){

        return new TypeFragment();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_type, container, false);
        return inflate;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ((TextView) view.findViewById(R.id.wl_tool_bar_search)).setText(LearnAdapter.TEXT_SEARCH);

        recyclerView = ((RecyclerView) view.findViewById(R.id.recycler_type_view));
        recyclerView_detail = ((RecyclerView) view.findViewById(R.id.recycler_type_view_detail));

        recyclerView_detail.setItemAnimator(new DefaultItemAnimator());
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        LinearLayoutManager manager1 = new LinearLayoutManager(getContext());

        manager.setOrientation(LinearLayoutManager.VERTICAL);
        manager1.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView_detail.setLayoutManager(manager1);

        detailAdapter = new DetailAdapter(getContext());
        recyclerView_detail.setAdapter(detailAdapter);
        typeAdapter = new TypeAdapter(getContext(),detailAdapter);
        recyclerView.setAdapter(typeAdapter);
        Observable<TypeListEntry> typeMainInfo = ServerUtil.createHttpRequest(Servers.class).getTypeList();
        typeMainInfo.observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io())
              .subscribe(new Subscriber<TypeListEntry>() {
                  @Override
                  public void onCompleted() {

                  }

                  @Override
                  public void onError(Throwable e) {

                  }

                  @Override
                  public void onNext(TypeListEntry typeListEntry) {

                      typeAdapter.addAll(typeListEntry.getData().getClassify());
                  }
              }) ;


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        MyApplication.main.findViewById(R.id.main_rg).setVisibility(View.VISIBLE);
        ImageView iv=  ((ImageView) MyApplication.main.findViewById(R.id.rl).findViewById(R.id.main_message));
        iv.setVisibility(View.INVISIBLE);
        iv.setImageResource(R.mipmap.app_images_navigationbar_navigation_email);

    }
}
